package com.lagou.mr.sort;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.lib.input.CombineTextInputFormat;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

import java.io.IOException;

public class NumberSortDriver {

    public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {

//        windows测试用输入输出路径
//        String inputPath = "d:\\numberSort\\input";
//        String outputPath = "d:\\numberSort\\output";
        String inputPath = args[0];
        String outputPath = args[1];

        // 1 获取配置信息，或者job对象实例
        Configuration conf = new Configuration();
        Job job = Job.getInstance(conf,"NumberSort");
        // 2 指定本程序的jar包所在的本地路径
        job.setJarByClass(NumberSortDriver.class);
        // 3 指定本业务job要使用的mapper/Reducer业务类
        job.setMapperClass(NumberSortMapper.class);
        job.setReducerClass(NumberSortReducer.class);
        // 4 指定mapper输出数据的kv类型
        job.setMapOutputKeyClass(NumberSortBean.class);
        job.setMapOutputValueClass(NullWritable.class);
        // 5 指定最终输出的数据的kv类型
        job.setOutputKeyClass(NullWritable.class);
        job.setOutputValueClass(NumberSortBean.class);
        // 6 指定job的输入原始文件所在目录
        job.setInputFormatClass(CombineTextInputFormat.class);
        CombineTextInputFormat.setMaxInputSplitSize(job,4194304);
        FileInputFormat.setInputPaths(job, new Path(inputPath));
        FileOutputFormat.setOutputPath(job, new Path(outputPath));
        //如果是打成jar后可以注释上面两行，用下面配置，通过输入参数执行
        //FileInputFormat.setInputPaths(job, new Path(args[0]));
        //FileOutputFormat.setOutputPath(job, new Path(args[1]));
        // 7 将job中配置的相关参数，以及job所用的java类所在的jar包， 提交给yarn去运行
        boolean result = job.waitForCompletion(true);
        System.exit(result ? 0 : 1);
    }
}
